package com.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.component.domain.MMenu;
import com.component.domain.SysRoleEntity;
import com.component.domain.SysRoleMenuEntity;
import com.component.domain.vo.SysRoleMenuVo;
import com.component.service.MMenuService;
import com.component.service.SysMenuService;
import com.component.service.SysRoleMenuService;
import com.component.service.SysRoleService;
import com.system.common.Result;
import com.system.security.IsAdmin;
import com.system.security.IsSecurity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.UsesSunHttpServer;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Classname SysRoleMenuApi
 * @Description TODO
 * @Date 2023/10/1 15:58
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Api(tags = {"角色菜单"})
@RestController
@RequestMapping("/sysrolemenu")
public class SysRoleMenuApi {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private MMenuService mMenuService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @IsAdmin
    @GetMapping("page")
    public JSONObject page(@RequestParam("pageNum")Integer pageNum,
                           @RequestParam("pageSize")Integer pageSize,
                           @RequestParam("roleId") String roleId){
        IPage<SysRoleMenuEntity> page=sysRoleMenuService.page(pageNum,pageSize,roleId);
        return Result.successInfo(page);
    }

    @IsAdmin
    @ApiOperation("获取所有菜单")
    @GetMapping("getallmenu")
    public JSONObject getAllMenus(){
        List<MMenu> menuList=mMenuService.parentList();
        for (int i = 0; i < menuList.size() ; i++) {
            MMenu menu = menuList.get(i);
            List<MMenu> menuList1=mMenuService.childrenList(menu.getId().toString());
            menu.setMMenuChildren(menuList1);
        }
        return Result.successInfo(menuList);
    }

    @IsAdmin
    @ApiOperation("添加角色菜单")
    @PostMapping("addRoleMenu")
    public JSONObject addRoleMenu(@RequestBody @Validated SysRoleMenuVo sysRoleMenuVo){
        SysRoleEntity sysRoleEntity=sysRoleService.getById(sysRoleMenuVo.getRoleId());
        if(sysRoleEntity == null){
            return Result.errorInfo("角色id不存在");
        }
        for(int i = 0; i < sysRoleMenuVo.getMenuId().size();i++){
            Long menuId=sysRoleMenuVo.getMenuId().get(i);
            MMenu mMenu=mMenuService.getById(menuId);
            if(mMenu==null){
                return Result.errorInfo("系统错误，请刷新后在试");
            }
            SysRoleMenuEntity sysRoleMenu=sysRoleMenuService.getSysRoleMenuEntityRidMid(sysRoleMenuVo.getRoleId(),mMenu.getId());
            if(sysRoleMenu!=null){
                continue;
            }
            SysRoleMenuEntity sysRoleMenuEntity=new SysRoleMenuEntity();
            sysRoleMenuEntity.setMenuId(Long.valueOf(String.valueOf(mMenu.getId())));
            sysRoleMenuEntity.setRoleId(sysRoleMenuVo.getRoleId());
            sysRoleMenuService.save(sysRoleMenuEntity);
        }
        return Result.successInfo("添加成功");
    }

    @IsAdmin
    @ApiOperation(value = "获取拥有哪些菜单")
    @GetMapping("getrolehasmenu")
    public JSONObject getrolehasmenu(@RequestParam("rid")Long rid){
        SysRoleEntity sysRoleEntity=sysRoleService.getById(rid);
        if(sysRoleEntity==null){
            return Result.errorInfo("系统错误, 请刷新后再试");
        }
        List<SysRoleMenuEntity> list=sysRoleMenuService.getSysRoleMenuEntityRid(rid);
        if(list==null){
            list= Collections.emptyList();
        }
        List<Long> longs=list.stream().map(e->e.getMenuId()).collect(Collectors.toList());
        return Result.successInfo(longs);
    }
}
